<template>
  <div class="app-container">
    <el-form :model="queryParams" style="display: flex; flex-wrap: nowrap;"
             ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名:" prop="F0103" style="display: flex; flex-wrap: nowrap;">
        <el-input
          style="width: 14vw"
          v-model="queryParams.F0103"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 50vw;">
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="t01List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号" align="center" prop="f0104" />
      <el-table-column label="姓名" align="center" prop="f0103" />
      <el-table-column label="电话" align="center" prop="f0108" />
      <el-table-column label="创建日期" align="center" prop="f0105" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.F0105, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" prop="f0106">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ygzt" :value="scope.row.f0106"/>
        </template>
      </el-table-column>
      <el-table-column label="权限" align="center" prop="f0107">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ygqx" :value="scope.row.f0107"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['t01:t01:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['t01:t01:remove']"
          >删除</el-button>
          <!-- 1启用，0禁用 -->
          <el-button size="mini" v-if="scope.row.f0106 === 1"
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['t01:t01:edit']"
            style="color: #b71c1c;"
          >禁用</el-button>
          <el-button size="mini" v-else
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['t01:t01:edit']"
          >启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="33vw" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="5.5vw">
        <el-form-item v-if="this.title==='添加员工管理'" label="账号" prop="f0104">
          <el-input v-model="form.f0104" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="f0102">
          <el-input v-model="form.f0102" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="姓名" prop="f0103">
          <el-input v-model="form.f0103" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="f0108">
          <el-input v-model="form.f0108" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="账号状态" prop="f0106">
          <el-radio-group v-model="form.f0106">
            <el-radio
              v-for="dict in dict.type.ygzt"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限" prop="f0107">
          <el-select v-model="form.f0107" placeholder="请选择权限">
            <el-option
              v-for="dict in dict.type.ygqx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listT01, getT01, delT01, addT01, updateT01 } from "@/api/t01/t01";

export default {
  name: "T01",
  dicts: ['ygqx', 'ygzt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工管理表格数据
      t01List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        F0103: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        f0102: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        f0103: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        f0104: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        f0106: [
          { required: true, message: "账号状态不能为空", trigger: "change" }
        ],
        f0107: [
          { required: true, message: "权限不能为空", trigger: "change" }
        ],
        f0108: [
          { required: true, message: "电话不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 根据传来的值对员工的状态进行修改 */
    changeStatus(row) {
      const a = row.f0101;
      const b = row.f0106;
      if (b === 1) {
        updateT01({ f0101: a, f0106: 0 }).then(response => {
          this.$modal.msgSuccess("禁用成功");
          this.getList();
        });
      } else {
        updateT01({f0101: a, f0106: 1 }).then(response => {
          this.$modal.msgSuccess("启用成功");
          this.getList();
        });
      }
    },
    /** 查询员工管理列表 */
    getList() {
      this.loading = true;
      listT01(this.queryParams).then(response => {
        this.t01List = response.rows;//数据
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        f0101: null,
        f0102: null,
        f0103: null,
        f0104: null,
        f0105: null,
        f0106: null,
        f0107: null,
        f0108: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.f0101)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const f0101 = row.f0101 || this.ids
      getT01(f0101).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.f0101 != null) {
            updateT01(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addT01(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const f0101s = row.f0101 || this.ids;
      this.$modal.confirm('是否确认删除员工管理编号为"' + f0101s + '"的数据项？').then(function() {
        return delT01(f0101s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('t01/t01/export', {
        ...this.queryParams
      }, `t01_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
/*  */
.el-tag-common-green {
    background-color: #E8FFEA; /* 浅绿色背景 */
    border-color: #98e69e; /* 深一些的绿色边框 */
    color: #66ffcc; /* 更亮的绿色字体 */
    display: inline-flex;
    justify-content: center;
    align-items: center;
    height: 24px;
    padding: 0 9px;
    font-size: 12px;
    line-height: 1;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
}

.el-tag-green {
    /* 假设您希望.el-tag-green与.el-tag-common完全一致，或者您可以指定不同的绿色 */
    background-color: #e0f2f1; /* 浅绿色背景 */
    border-color: #b2d7d2; /* 深一些的绿色边框 */
    color: #4fdc4f; /* 更亮的绿色字体 */
    /* 如果需要特别指定.el-tag-green独有的样式，可以在这里添加 */
}

/* 红 */
.el-tag-common-red {
    background-color: #ffebee; /* 浅红色背景 */
    border-color: #d32f2f; /* 深红色边框 */
    color: #b71c1c; /* 深红色字体 */
    display: inline-flex;
    justify-content: center;
    align-items: center;
    height: 24px;
    padding: 0 9px;
    font-size: 12px;
    line-height: 1;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
}
.el-tag-red {
    /* 重写.el-tag-common的样式以使用红色主题 */
    background-color: #ffebee; /* 浅红色背景 */
    border-color: #d32f2f; /* 深红色边框 */
    color: #b71c1c; /* 深红色字体 */
    /* 如果需要特别指定.el-tag-green独有的样式，可以在这里添加 */
}
</style>
